package com.workplan.client;

import net.customware.gwt.presenter.client.DefaultEventBus;
import net.customware.gwt.presenter.client.EventBus;

import com.general.client.bundle.CommonBundle;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;
import com.workplan.client.bundle.WorkPlanBundle;
import com.workplan.client.modules.Modules;
import com.workplan.client.modules.defineworkweek.DefineWorkweek;
import com.workplan.client.modules.defineworkweek.DefineWorkweekPresenter;
import com.workplan.client.modules.defineworkweek.DefineWorkweekPresenterImpl;
import com.workplan.client.modules.events.SwitchModuleEvent;
import com.workplan.client.modules.staff.AssembleStaff;
import com.workplan.client.modules.staff.AssembleStaffPresenter;
import com.workplan.client.modules.staff.AssembleStaffPresenterImpl;
import com.workplan.client.widgets.leftpanel.LeftPanel;
import com.workplan.client.widgets.leftpanel.LeftPanelPresenter;
import com.workplan.client.widgets.leftpanel.LeftPanelPresenterImpl;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class WorkPlan implements EntryPoint {

	private AssembleStaffPresenter.Display assembleStaff;
	private AssembleStaffPresenter assembleStaffPresenter = null;

	private DefineWorkweekPresenter.Display defineWorkweek;
	private DefineWorkweekPresenter defineWorkweekPresenter = null;

	private Modules selectedModule;

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {
		GWT.<CommonBundle> create(CommonBundle.class).style().ensureInjected();
		GWT.<CommonBundle> create(CommonBundle.class).text().ensureInjected();

		GWT.<WorkPlanBundle> create(WorkPlanBundle.class).style()
				.ensureInjected();
		GWT.<WorkPlanBundle> create(WorkPlanBundle.class).text()
				.ensureInjected();

		EventBus eventBus = new DefaultEventBus();

		LeftPanel leftPanel = new LeftPanel();
		LeftPanelPresenter leftPanelPresenter = new LeftPanelPresenterImpl(
				leftPanel, eventBus);
		leftPanelPresenter.bind();

		RootPanel.get("leftPanel").add(
				leftPanelPresenter.getDisplay().asWidget());

		modulesSwitcher(eventBus, RootPanel.get("mainPanel"));

		eventBus.fireEvent(new SwitchModuleEvent(Modules.STAFF));
	}

	private void modulesSwitcher(final EventBus eventBus,
			final RootPanel container) {
		eventBus.addHandler(SwitchModuleEvent.TYPE,
				new SwitchModuleEvent.Handler() {

					@Override
					public void onSwitchModule(SwitchModuleEvent event) {
						if (event.getNewModule().equals(selectedModule))
							return;
						selectedModule = event.getNewModule();
						container.clear();
						if (event.getNewModule().equals(Modules.STAFF)) {
							if (assembleStaffPresenter == null) {
								assembleStaff = new AssembleStaff();
								assembleStaffPresenter = new AssembleStaffPresenterImpl(
										assembleStaff, eventBus);
								assembleStaffPresenter.bind();
							}
							container.add(assembleStaffPresenter.getDisplay()
									.asWidget());
						} else if (event.getNewModule()
								.equals(Modules.WORKWEEK)) {
							if (defineWorkweekPresenter == null) {
								defineWorkweek = new DefineWorkweek();
								defineWorkweekPresenter = new DefineWorkweekPresenterImpl(
										defineWorkweek, eventBus);
								defineWorkweekPresenter.bind();
							}
							container.add(defineWorkweekPresenter.getDisplay()
									.asWidget());
						}

					}
				});

	}
}
